<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>代付订单</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">代付订单</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row">
                    <div class="layui-form" style="float:right; margin-bottom: 10px;">
                        <div class="layui-form-item" style="margin:0;">
                            <div class="layui-input-inline">
                                <input type="text" name="createTimeStart" id="createTimeStart" autocomplete="off" placeholder="开始时间" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="createTimeEnd" id="createTimeEnd" autocomplete="off" placeholder="结束时间" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="accountName" id="accountName" placeholder="账户名" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="agentpayOrderId" id="agentpayOrderId" placeholder="代付单号" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="transOrderId" id="transOrderId" placeholder="转账单号" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <select name="agentpayChannel" id="agentpayChannel" lay-search="">
                                    <option value="">代付渠道</option>
                                    <option value="1">商户后台</option>
                                    <option value="2">API接口</option>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select name="status" id="status" lay-search="">
                                    <option value="">状态</option>
                                    <option value="0">待处理</option>
                                    <option value="1">处理中</option>
                                    <option value="2">成功</option>
                                    <option value="3">失败</option>
                                </select>
                            </div>
                            <button id="search" class="layui-btn" data-type="reload">搜索</button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <blockquote class="layui-elem-quote" id="amountTip">
                            订单数:<sapn id="allTotalCount" style="color: blue; margin-right: 10px;"></sapn>
                            代付金额:<span id="allTotalAmount" style="color: blue; margin-right: 10px;"></span>
                            代付费用:<span id="allTotalFee" style="color: green; margin-right: 10px;"></span>
                            扣减金额:<span id="allTotalSubAmount" style="color: green; margin-right: 10px;"></span>
                        </blockquote>
                    </div>
                </div>
                <table id="jsList"  lay-filter="jsList"></table>
            </div>
        </div>
    </div>
</div>
<script>
     //标题内容模板
     var tplEdit = function(d){
         return "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='detail'>详情</a>" +
                     "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='transResult'>上游结果</a>";
     };

     var tplStatus = function(d){
         if(d.status == 0) {
             return '<span class="layui-badge layui-bg-blue">待处理</span>';
         }else if(d.status == 1) {
             return '<span class="layui-badge layui-bg-orange">处理中</span>';
         }else if(d.status == 2) {
             return '<span class="layui-badge layui-bg-green">成功</span>';
         }else if(d.status == 3) {
             return '<span class="layui-badge">失败</span>';
         }
     };

     var tplAgentpayChannel = function(d){
         if(d.agentpayChannel == 1) {
             return '商户后台';
         }else if(d.agentpayChannel == 2) {
             return 'API接口';
         }
     };

     layui.use(['table','util','admin', 'laydate'], function(){
        var table = layui.table
        ,$ = layui.$
        ,admin = layui.admin
        ,laydate = layui.laydate
        ,element = layui.element
        ,form = layui.form;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

         $.setCountResult = function(mchId, accountName, agentpayOrderId, transOrderId, status, agentpayChannel, createTimeStart, createTimeEnd) {
             admin.req({
                 type: 'get',
                 url: layui.setter.baseUrl + '/agentpay/count',
                 data: {
                     mchId : mchId,
                     accountName : accountName,
                     agentpayOrderId : agentpayOrderId,
                     transOrderId : transOrderId,
                     status : status,
                     agentpayChannel: agentpayChannel,
                     createTimeStart : createTimeStart,
                     createTimeEnd : createTimeEnd
                 },
                 error: function(err){
                     layer.alert(JSON.stringify(err.field), {
                         title: '错误提示'
                     })
                 },
                 success: function(res){
                     if(res.code == 0){
                         $('#allTotalCount').html(res.data.allTotalCount);
                         $('#allTotalAmount').html("￥" + res.data.allTotalAmount/100.00);
                         $('#allTotalFee').html("￥" + res.data.allTotalFee/100.0);
                         $('#allTotalSubAmount').html("￥" + res.data.allTotalSubAmount/100.00);
                     }
                 }
             });
         };

         // 初始化统计结果
         $.setCountResult();

        table.render({
            elem: '#jsList'
            ,id: "tableReload"
            ,url: layui.setter.baseUrl + '/agentpay/list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,cols: [[
                {field: 'mchId', title: '商户ID'}
                ,{field: 'transOrderId', width: 220, title: '转账单号'}
                ,{field: 'accountName',title: '账户名'}
                ,{field: 'accountNo',title: '账号'}
                ,{field: 'amount', title: '代付金额(元)',templet: '<div>{{d.amount/100}}</div>' }
                ,{field: 'fee', title: '手续费(元)',templet: '<div>{{d.fee/100}}</div>' }
                ,{field: 'fee', title: '扣减账户金额(元)',templet: '<div>{{d.subAmount/100}}</div>' }
                ,{field: 'status',title: '状态',templet: tplStatus}
                ,{field: 'agentpayChannel',title: '代付渠道',templet: tplAgentpayChannel}
                ,{field: 'createTime',  title: '时间', width: 180, templet: '<div>{{ layui.util.toDateString(d.createTime) }}</div>' }
                ,{field: 'edite', title: '操作', templet: tplEdit }
            ]]
            ,page: true
            ,skin: 'line'
        });

         //监听工具条
         table.on('tool(jsList)', function(obj){
             var data = obj.data;
             if(obj.event === 'detail'){
                 location.href = layui.setter.baseLocal + "agentpay/list/detail/agentpayOrderId=" + data.agentpayOrderId;
             } else if(obj.event == 'transResult') {
                 if(data.transOrderId == null || data.transOrderId == '') {
                     layer.alert("没有转账订单号,不能查询");
                     return;
                 }
                 layer.msg('正在查询代付通道代付结果...', {icon: 16});
                 admin.req({
                     type: 'post',
                     url: layui.setter.baseUrl + '/agentpay/trans_query',
                     data: {
                         transOrderId: data.transOrderId
                     },
                     error: function(err){
                         layer.alert(err);
                     },
                     success: function(res){
                         layer.closeAll('loading');
                         if(res.code == 0){
                             layer.open({
                                 title: '订单:' + data.agentpayOrderId
                                 ,content: res.data
                             });
                         }
                     }
                 });
             }
         });

         // 搜索
         var $ = layui.$, active = {
             reload: function(){
                 var createTimeStart = $('#createTimeStart').val();
                 var createTimeEnd = $('#createTimeEnd').val();
                 var agentpayOrderId = $('#agentpayOrderId').val();
                 var transOrderId = $('#transOrderId').val();
                 var accountName = $('#accountName').val().trim();;
                 var mchId = $('#mchId').val();
                 var agentpayChannel = $("#agentpayChannel").val();
                 var status = $("#status").val();
                 $.setCountResult(mchId, accountName, agentpayOrderId, transOrderId, status, agentpayChannel, createTimeStart, createTimeEnd);
                 //执行重载
                 table.reload('tableReload', {
                     page: {
                         curr: 1 //重新从第 1 页开始
                     }
                     ,where: {
                         accountName: accountName,
                         agentpayOrderId: agentpayOrderId,
                         transOrderId: transOrderId,
                         mchId: mchId,
                         createTimeStart : createTimeStart,
                         createTimeEnd : createTimeEnd,
                         agentpayChannel: agentpayChannel,
                         status : status
                     }
                 });
             }
         };
         $('#search').on('click', function() {
             var type = $(this).data('type');
             active[type] ? active[type].call(this) : '';
         });

         laydate.render({
             elem: '#createTimeStart'
             ,type: 'datetime'
             ,format: 'yyyy-MM-dd HH:mm:ss'
         });

         laydate.render({
             elem: '#createTimeEnd'
             ,type: 'datetime'
             ,format: 'yyyy-MM-dd HH:mm:ss'
         });

         // 渲染表单
         form.render();

     });


</script>